package com.py.product.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.product.domain.SysDictData;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.product.domain.ProductType;
import com.py.product.service.IProductTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 货品类型Controller
 * 
 * @author he
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/type")
public class ProductTypeController extends BaseController
{
    @Autowired
    private IProductTypeService productTypeService;

    @Resource
    private PyCodeApi pyCodeApi;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        return pyCodeApi.generateCode(code);
    }


    /**
     * 查询货品类型列表
     */
    @RequiresPermissions("product:type:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductType productType)
    {
        startPage();
        List<ProductType> list = productTypeService.selectProductTypeList(productType);
        return getDataTable(list);
    }

    /**
     * 导出货品类型列表
     */
    @RequiresPermissions("product:type:export")
    @Log(title = "货品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductType productType)
    {
        List<ProductType> list = productTypeService.selectProductTypeList(productType);
        ExcelUtil<ProductType> util = new ExcelUtil<ProductType>(ProductType.class);
        util.exportExcel(response, list, "货品类型数据");
    }

    /**
     * 获取货品类型详细信息
     */
    @RequiresPermissions("product:type:query")
    @GetMapping(value = "/{productTypeId}")
    public AjaxResult getInfo(@PathVariable("productTypeId") Long productTypeId)
    {
        return success(productTypeService.selectProductTypeByProductTypeId(productTypeId));
    }

    /**
     * 新增货品类型
     */
    @RequiresPermissions("product:type:add")
    @Log(title = "货品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductType productType)
    {
        System.out.println("number:"+productType.getNumber());
        System.out.println("codeId:"+productType.getCodeId());
        pyCodeApi.saveCode(productType.getCodeId(),productType.getNumber());
        productType.setCreateBy(SecurityUtils.getUsername());
        productType.setCreateTime(new Date());
        return toAjax(productTypeService.insertProductType(productType));
    }

    /**
     * 修改货品类型
     */
    @RequiresPermissions("product:type:edit")
    @Log(title = "货品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductType productType)
    {
        return toAjax(productTypeService.updateProductType(productType));
    }

    /**
     * 删除货品类型
     */
    @RequiresPermissions("product:type:remove")
    @Log(title = "货品类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productTypeIds}")
    public AjaxResult remove(@PathVariable Long[] productTypeIds)
    {
        return toAjax(productTypeService.deleteProductTypeByProductTypeIds(productTypeIds));
    }


    @RequestMapping("/findAll")
    public List<ProductType> findAll(){
        System.out.println("11111");
        System.out.println(productTypeService.selectTypeListAll());
        return productTypeService.selectTypeListAll();
    }

     @RequestMapping("/findParent")
    public List<ProductType> findParent(@RequestParam("parentId") String parentId){
         System.out.println("11111"+productTypeService.findParent(parentId));
        return productTypeService.findParent(parentId);
    }

    /**
     * 根据类型查字典
     */
    @RequestMapping("/selectAllByDictType")
    public List<SysDictData> selectAllByDictType(@RequestParam("dictType") String dictType){
        System.out.println(dictType);
        System.out.println(productTypeService.selectAllByDictType(dictType));
        return productTypeService.selectAllByDictType(dictType);
    }
}
